package com.dooboolab.fluttersound;


import android.content.Context;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

class FlutterSoundPlayerManager extends FlutterSoundManager
        implements MethodCallHandler {
    final static String TAG = "FlutterPlayerPlugin";
    static Context androidContext;
    static FlutterSoundPlayerManager flutterSoundPlayerPlugin; // singleton


    public static void attachFlautoPlayer(
            Context ctx, BinaryMessenger messenger
    ) {
        if (flutterSoundPlayerPlugin == null) {
            flutterSoundPlayerPlugin = new FlutterSoundPlayerManager();
        }
        MethodChannel channel = new MethodChannel(messenger, "com.dooboolab.flutter_sound_player");
        flutterSoundPlayerPlugin.init(channel);
        channel.setMethodCallHandler(flutterSoundPlayerPlugin);
        androidContext = ctx;
    }


    FlutterSoundPlayerManager getManager() {
        return flutterSoundPlayerPlugin;
    }

    @Override
    public void onMethodCall(final MethodCall call, final Result result) {
        switch (call.method) {
            case "resetPlugin": {
                resetPlugin(call, result);
                return;
            }
        }

        FlutterSoundPlayer aPlayer = (FlutterSoundPlayer) getSession(call);
        switch (call.method) {
            case "openPlayer": {
                aPlayer = new FlutterSoundPlayer(call);
                initSession(call, aPlayer);
                aPlayer.openPlayer(call, result);

            }
            break;

            case "closePlayer": {
                aPlayer.closePlayer(call, result);
            }
            break;

            case "isDecoderSupported": {
                aPlayer.isDecoderSupported(call, result);
            }
            break;


            case "getPlayerState": {
                aPlayer.getPlayerState(call, result);
            }
            break;

            case "getResourcePath": {
                aPlayer.getResourcePath(call, result);
            }
            break;


            case "getProgress": {
                aPlayer.getProgress(call, result);
            }
            break;

            case "startPlayer": {
                aPlayer.startPlayer(call, result);
            }
            break;

            case "startPlayerFromMic": {
                aPlayer.startPlayerFromMic(call, result);
            }
            break;


            case "stopPlayer": {
                aPlayer.stopPlayer(call, result);
            }
            break;


            case "pausePlayer": {
                aPlayer.pausePlayer(call, result);
            }
            break;

            case "resumePlayer": {
                aPlayer.resumePlayer(call, result);
            }
            break;

            case "seekToPlayer": {
                aPlayer.seekToPlayer(call, result);
            }
            break;

            case "setVolume": {
                aPlayer.setVolume(call, result);
            }
            break;

            case "setSpeed": {
                aPlayer.setSpeed(call, result);
            }
            break;

            case "setSubscriptionDuration": {
                aPlayer.setSubscriptionDuration(call, result);
            }
            break;


            case "feed": {
                aPlayer.feed(call, result);
            }
            break;

            case "setLogLevel": {
                aPlayer.setLogLevel(call, result);
            }
            break;

            default: {
                result.notImplemented();
            }
            break;
        }
    }

}
